package activiti;

import java.util.HashMap;
import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

/**
 * 排他网关
 * @author Shea
 *
 */
public class Gateway {
	//全局定义流程引擎对象
	ProcessEngine processEngine =ProcessEngines.getDefaultProcessEngine();
	/*
	 * 部署流程
	 */
	@Test
	public void deployment(){
		DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
		//加载流程文件
		deploymentBuilder.addClasspathResource("gateway.bpmn");
		deploymentBuilder.addClasspathResource("gateway.png");
		//部署
		Deployment deploy = deploymentBuilder.deploy();
	}
	/*
	 * 启动流程---根据流程定义id或者key,需要先查询流程定义key或id
	 */
	@Test
	public void startInstance(){
		ProcessDefinitionQuery definitionQuery = processEngine.getRepositoryService().createProcessDefinitionQuery();
		//definitionQuery.list();
		processEngine.getRuntimeService().startProcessInstanceByKey("fee");
	}
	/*
	 * 查询任务id并处理流程---并设置变量
	 */
	@Test
	public void dealTask(){
		String taskId = processEngine.getTaskService().createTaskQuery().list().get(0).getId();//因为当前任务就一个,所以可以直接通过查询get获得
		HashMap<String, Object> variables = new HashMap<>();
		variables.put("money", 5000);
		processEngine.getTaskService().complete(taskId,variables);
	}
	/*
	 *查询任务
	 */
	@Test
	public void getTask(){
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
		List<Task> list = taskQuery.list();
		for (Task task : list) {
			System.out.println(task.getId()+"=="+task.getName());
		}
	}
}
